#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <stdint.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "st7789.h"
#include "bmp_decoder.h"
#include "DevInfo.h"
#include "NetTools.h"
#include "UserCfg.h"


#define TEMP_STR_LEN    64
#define IP_STR_LEN      20
#define TO_GB(b)        ((b) / pow(1024, 3))
#define TO_SEC(h, m)    ((h) * 3600 + (m) * 60)
#define BUTTON_PIN 17

extern const uint16_t gImage_mickey1[];
char tempStr[TEMP_STR_LEN];
char ipStr[IP_STR_LEN];

unsigned int freeMem, totalMem;


//用于CPU利用率计算
CPU_OCCUPY cpu_stat1;           
CPU_OCCUPY cpu_stat2;
float cpuUsage;

//用于内存利用率计算
unsigned int freeMem, totalMem;

//用于网络速率计算
long int start_rcv_rates = 0;   //保存开始时的流量计数
long int end_rcv_rates = 0;	    //保存结束时的流量计数
long int start_tx_rates = 0;    //保存开始时的流量计数
long int end_tx_rates = 0;      //保存结束时的流量计数
float tx_rates = 0;             //上传速度Bytes/s
float rx_rates = 0;             //下载速度Bytes/s
Net_State wlanState;
Net_State ethState;
char netSpeedUnit[4][5] = {
    " B/s",
    "KB/s",
    "MB/s",
    "GB/s",
};

void CalcBasic(void)
{
	GetCurNetFlow(wlanState == STATE_CONNECT ? WLAN_IF : (ethState == STATE_CONNECT ? ETH_IF : NULL),
    &start_rcv_rates, &start_tx_rates);
	get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);
    sleep(1);
    GetCurNetFlow(wlanState == STATE_CONNECT ? WLAN_IF : (ethState == STATE_CONNECT ? ETH_IF : NULL),
    &end_rcv_rates, &end_tx_rates);
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);
    
    cpuUsage = cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1,(CPU_OCCUPY *)&cpu_stat2);
    rx_rates = (float)(end_rcv_rates - start_rcv_rates);
    tx_rates = (float)(end_tx_rates - start_tx_rates);
}

void NetWork(void)
{
    ST7789_WriteString(5,10,"                 ",Font_11x18,WHITE,BLACK);
    wlanState = GetWirelessState();
    //ethState = GetEthernetState();    
    if (wlanState == STATE_CONNECT)
    {
        memset(ipStr, 0, IP_STR_LEN);
        int ret = 0;
        ret = GetLocalIP(WLAN_IF, ipStr);
        if (ret != 0)   memset(ipStr, 0, IP_STR_LEN);
        ST7789_WriteString(5,10,ipStr,Font_11x18,BLUE,BLACK);
    }
    else if (ethState == STATE_CONNECT)
    {
        memset(ipStr, 0, IP_STR_LEN);
        int ret = 0;
        ret = GetLocalIP(ETH_IF, ipStr);
        if (ret != 0)   memset(ipStr, 0, IP_STR_LEN);
        ST7789_WriteString(5,10,ipStr,Font_11x18,BLUE,BLACK);
    }
    else
    {
        ST7789_WriteString(5,10,"Not Error      ",Font_11x18,RED,BLACK);
    }	
}
int main()
{
    wiringPiSetup();
    //控制背光的引脚，高电平点亮背光，不需要可以不用
    pinMode(13, OUTPUT);
    pinMode(BUTTON_PIN, INPUT);
    pullUpDnControl(BUTTON_PIN, PUD_UP);
    digitalWrite(13, HIGH);

    ST7789_Init();  //初始化屏幕，初始化的配置请看st7789.h
    ST7789_Fill_Color(BLACK);
    //ST7789_DrawImage(20, 20, 108, 128, (uint16_t*)gImage_mickey1);
    //ST7789_WriteString(140,20,"OrangePiZero3",Font_11x18,WHITE,BLACK);
    //ST7789_WriteString(140,128,"JunrLi",Font_7x10,WHITE,BLACK);
    //ST7789_WriteString(140,138,"1396039762@qq.com",Font_7x10,WHITE,BLACK);
    
    DecodeRet ret = -1;
    ShowBMPImage("/home/orangepi/Desktop/my_code/ST7789_for_wiringPi-main/ST7789_for_wiringPi-main/BMP/ubuntu_320_172.bmp", 0, 0, &ret);
    sleep(5);
    ST7789_Fill_Color(BLACK);
    ST7789_DrawLine(5,80,180,80,WHITE);
    ST7789_DrawLine(5,130,180,130,WHITE);

    ST7789_WriteString(5,10,"Disk:         GB|",Font_11x18,WHITE,BLACK);
    
    ST7789_WriteString(5,45,"CPU:      %",Font_16x26,WHITE,BLACK);
      
    ST7789_WriteString(5,95,"Mem:      %",Font_16x26,WHITE,BLACK);
        
    ST7789_WriteString(5,145,"Temp:     C",Font_16x26,WHITE,BLACK);
    
    //ST7789_DrawImage(5, 145, 16, 16, (uint16_t*)gImage_mickey1);
  while(1)
   {
    //驱动库自带的测试函数
    //ST7789_Test();
    //测试显示取模后的图片
    //ST7789_Fill_Color(BLACK);
    //ST7789_DrawImage(0, 0, 320, 172, (uint16_t*)gImage_ys_320_172);
    //sleep(9);
      //ST7789_DrawImage(190, 40, 128, 128, (uint16_t*)gImage_mickey1);
    DecodeRet ret = -1;
    /*这里需要添加绝对路径，否则显示异常*/
    ShowBMPImage("/home/orangepi/Desktop/my_code/ST7789_for_wiringPi-main/ST7789_for_wiringPi-main/BMP/JunrLi.bmp", 190, 40, &ret);
    CalcBasic();

	//CPU占用率
    memset(tempStr, 0, TEMP_STR_LEN);
    sprintf(tempStr, "%.1f", cpuUsage);
    ST7789_WriteString(88,45,tempStr,Font_16x26,WHITE,BLACK);
    
    //内存占用率
    freeMem = GetMemFree();
    totalMem = GetMemTotal();
    memset(tempStr, 0, TEMP_STR_LEN);
    sprintf(tempStr, "%.1lf %d",TO_GB(totalMem - freeMem),
            (int)((totalMem - freeMem) * 1.0 / totalMem * 100));
    ST7789_WriteString(70,95,tempStr,Font_16x26,WHITE,BLACK);
    
    //网络速率
    uint8_t rxUnitLevel = 0;
    uint8_t txUnitLevel = 0;
    while (tx_rates >= 1000)
    {
        tx_rates /= 1000;
        txUnitLevel++;
    }
    while (rx_rates >= 1000)
    {
        rx_rates /= 1000;
        rxUnitLevel++;
    }
    memset(tempStr, 0, TEMP_STR_LEN);//发送速率
    sprintf(tempStr, "  Up:%.1lf   ",tx_rates);
    ST7789_WriteString(200,10,tempStr,Font_7x10,WHITE,BLACK);
    memset(tempStr, 0, TEMP_STR_LEN);
    sprintf(tempStr, "%s",netSpeedUnit[txUnitLevel]);
    ST7789_WriteString(280,10,tempStr,Font_7x10,WHITE,BLACK);
    memset(tempStr, 0, TEMP_STR_LEN);//接收速率
    sprintf(tempStr, "Down:%.1lf   ",rx_rates);
    ST7789_WriteString(200,20,tempStr,Font_7x10,WHITE,BLACK);
    memset(tempStr, 0, TEMP_STR_LEN);
    sprintf(tempStr, "%s",netSpeedUnit[rxUnitLevel]);
    ST7789_WriteString(280,20,tempStr,Font_7x10,WHITE,BLACK);
    
    //CPU温度
    memset(tempStr, 0, TEMP_STR_LEN);
    sprintf(tempStr, "%.1f", GetCpuTemp());
    ST7789_WriteString(88,145,tempStr,Font_16x26,WHITE,BLACK);

    //按键检测
        if (digitalRead(BUTTON_PIN) == HIGH)
    {
        /* code */
        NetWork();
        sleep(3);
        ST7789_WriteString(5,10,"Disk:         GB|",Font_11x18,WHITE,BLACK);
    }
    else{
        //磁盘剩余容量
        memset(tempStr, 0, TEMP_STR_LEN);
        sprintf(tempStr, "%.2f", (float_t)GetAvailDisk() / 1000);
        ST7789_WriteString(85,10,tempStr,Font_11x18,WHITE,BLACK);
    }
    
   }
    //BMP图像解码测试
//    ST7789_Fill_Color(BLACK);
//    DecodeRet ret = -1;
//    ShowBMPImage("./ys_logo.bmp", 0, 0, &ret);
//    printf("%d\n", ret);
}
